package przykladyWykresow;

import java.awt.Color;
import java.util.HashMap;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

@SuppressWarnings("serial")
public class WykresySinusoidalne extends ChartPanelAbstract {
	private final static int min = -2;
	private final static int max = 2;
	private final static int count = 1000;
	private String title = null;
	private static XYSeriesCollection set1;
	private static XYSeriesCollection set2;

	public WykresySinusoidalne(final String _title) {
		super(createChart(_title));
		setTitle(_title);
	}

	private static double fun1(double x) {
		return Math.sin(x) + 1;
	}

	private static double fun2(double x) {
		return Math.cos(x);
	}

	private static void createDataset() {
		set1 = new XYSeriesCollection();
		set2 = new XYSeriesCollection();
		final XYSeries sin1 = new XYSeries("sin1");
		final XYSeries sin2 = new XYSeries("sin2");
		final XYSeries scatter1 = new XYSeries("Nad wykresem sin1");
		final XYSeries scatter2 = new XYSeries("Pod wykresem sin2");
		final XYSeries scatter3 = new XYSeries("Pomiędzy wykresami");
		Random randomGenerator = new Random();
		for (int i = 0; i <= count; i++) {
			double x = (float) i / 100;
			double y = (randomGenerator.nextFloat() * (max - min)) + min;
			sin1.add(x, fun1(x));
			sin2.add(x, fun2(x));
			if (y >= fun1(x)) {
				if (y >= fun2(x))
					scatter1.add(x, y);
				else
					scatter3.add(x, y);
			} else if (y >= fun2(x)) {
				scatter3.add(x, y);
			} else {
				scatter2.add(x, y);
			}
		}

		set1.addSeries(sin1);
		set1.addSeries(sin2);
		set2.addSeries(scatter1);
		set2.addSeries(scatter2);
		set2.addSeries(scatter3);

	}

	public static JFreeChart createChart(String _title) {
		createDataset();
		final JFreeChart chart = ChartFactory.createXYLineChart(_title, "X",
				"Y", set1, PlotOrientation.VERTICAL, true, true, false);

		chart.setBackgroundPaint(Color.white);
		final XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.lightGray);
		final XYDifferenceRenderer renderer = new XYDifferenceRenderer(
				new Color(255, 0, 0, 128), new Color(0, 255, 0, 128), false);
		renderer.setSeriesPaint(0, Color.green);
		renderer.setSeriesPaint(1, Color.blue);
		plot.setRenderer(renderer);
		plot.setDataset(1, set2);
		plot.mapDatasetToRangeAxis(1, 0);
		final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
		renderer2.setSeriesLinesVisible(0, false);
		renderer2.setSeriesLinesVisible(1, false);
		renderer2.setSeriesLinesVisible(2, false);
		renderer2.setSeriesPaint(0, Color.green);
		renderer2.setSeriesPaint(1, Color.blue);
		renderer2.setSeriesPaint(2, Color.red);
		plot.setRenderer(1, renderer2);
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		return chart;

	}

	@Override
	public void changeData() {
		this.setChart(createChart(getTitle()));
	}

	@Override
	public String getTitle() {
		return this.title;
	}

	@Override
	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public HashMap<String, Integer> getHashMapDataSet() {

		return null;
	}

	@Override
	public void setHashMapDataSet(HashMap<String, Integer> hashMap) {

	}

}
